<template>
  <div class="login-container">
    <el-form class="myForm" :model="form" :rules="rules" ref="myForm">
      <h3>iHRM后台登录系统</h3>
      <el-form-item prop="mobile">
        <el-input
          placeholder="请输入用户名"
          prefix-icon="el-icon-user-solid"
          v-model="form.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          placeholder="请输入密码"
          show-password
          prefix-icon="el-icon-lock"
          v-model="form.password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" round @click="toLogin"
          >登录</el-button
        >
      </el-form-item>
      <div class="mark">还没有账号？立即注册</div>
    </el-form>
  </div>
</template>

<script>
//导入mapAction辅助函数
import { mapActions } from 'vuex'

export default {
  data () {
    return {
      //用户信息
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      //手机号与密码的校验规则
      rules: {
        mobile: [
          //手机号为必填
          { required: true, message: '请输入手机号', trigger: 'blur' },
          //手机号的正则验证
          {
            validator: function (rule, vlaue, callback) {
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              //测试正则
              reg.test(vlaue)
                ? callback()
                : callback(new Error('手机号格式不正确'))
            }
          }
        ],
        //密码的校验
        password: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度为6-16个字符', trigger: 'blur' }
        ]
      },
      //默认加载动画是false状态
      loading: false
    }
  },
  //给登录按钮添加的点击事件
  methods: {
    //获取到user.js中actions中的方法
    ...mapActions({
      userLogin: 'user/login'
    }),
    toLogin () {
      //验证表单中的数据
      this.$refs.myForm.validate(async valid => {
        if (valid) {
          //登录成功后需要开启加载动画
          this.loading = true
          try {
            //验证通过
            await this.userLogin(this.form) //发送网络请求
            //接收permission中传过来的_retirect
            const _retirect = this.$route.query._retirect
            //在判断是否存在参数_redirect
            if (_retirect) {
              this.$router.push(_redirect)
            } else {
              //不存在就跳转回首页
              this.$router.push('/')
            }
            //验证通过只会就需要跳转到主页
            this.$router.push('/')
            //成功跳转到主页后提示登录成功
            this.$message.success('欢迎回家，家人们')
          } catch (error) {
            console.log(error.message)
          } finally {
            //关闭加载动画
            this.loading = false
          }
        } else {
          //校验失败
          alert('信息输入错误，请重新输入')
        }
      })
    }
  }
}
</script>

<style lang="scss">
//版心盒子
.login-container {
  height: 100%;
  background-image: url('../../assets/common/login.jpg');
  background-position: center;
  //最大的form表单
  .myForm {
    width: 520px;
    height: 400px;
    padding: 35px 35px 15px 35px;
    position: fixed;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
    //标题
    h3 {
      color: #dce6fe;
      font-size: 40px;
      letter-spacing: 7px;
    }
    //输入框
    .el-input__inner {
      height: 55px;
      //去除红色边框
      border-color: transparent;
      outline: transparent;
    }
    //登录按钮
    .el-button {
      width: 100%;
      background-color: #407ffe;
      height: 55px;
    }
    //最下方的提示文本
    .mark {
      font-size: 20px;
      color: #dce6fe;
      text-align: center;
    }
    .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
